﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Fodee.BusinessComponent;

namespace Fodee
{
    public partial class FoodSearch : Form
    {
        public FoodSearch()
        {
            InitializeComponent();
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            this.SearchFood();
        }

        private void grdFoodList_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case (char)Keys.Up:
                    break;

                
                case (char)Keys.Enter:
                    //this.grdFoodList.Rows[this.grdFoodList.SelectedRows[0].Index - 1].Selected = true;
                    this.OrderFood();
                    break;
                default:
                    break;
            }
        }

        private void grdFoodList_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            txtPrice.Text = grdFoodList.Rows[e.RowIndex].Cells["Price"].Value.ToString();
            txtCount.Text = "1";
        }

        private void FoodSearch_Load(object sender, EventArgs e)
        {
            this.SearchFood();
        }

        private void SearchFood()
        {
            FoodBC fbc = new FoodBC();
            grdFoodList.AutoGenerateColumns = false;
            grdFoodList.DataSource = fbc.SearchFood(txtSearch.Text).fod_Food.DefaultView;
            
        }

        private void OrderFood()
        {
            if (grdFoodList.Rows.Count > 0)
            {
                int id = int.Parse(grdFoodList.SelectedRows[0].Cells["ID"].Value.ToString());
                string code = grdFoodList.SelectedRows[0].Cells["code"].Value.ToString();
                string name = grdFoodList.SelectedRows[0].Cells["name"].Value.ToString();
                int count = int.Parse(txtCount.Text);
                string unit = grdFoodList.SelectedRows[0].Cells["unit"].Value.ToString();
                string remark = cbxRemark.Text;
                decimal price = decimal.Parse(txtPrice.Text);
                bool isFree = chxIsFree.Checked ? true : false;
                ((OrderDetail)this.Owner).AddFood(id, code, name, count, unit, price, remark, isFree);
            }
        }

        private void grdFoodList_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.OrderFood();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                this.grdFoodList_CellMouseDoubleClick(this.grdFoodList, null);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void FoodSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case (char)Keys.Enter:
                    this.grdFoodList.Focus();
                    break;
                case (char)Keys.Escape:
                    this.Close();
                    break;
            }
        }
    }
}